<?php
namespace app\admin666\controller;

use think\Db;
use think\Validate;

class Article extends Admin
{
    /**
    * 文档列表
    * @date: 2018年8月16日 上午10:22:45
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function index($tid){
	    $tid = intval($tid);
	    $display = trim(input('display',''));
	    $q = trim(input('get.q',''));
        
        $map = [];
        
	    if($q != ''){
	    	$map['title'] = ['like',"%{$q}%"];
	    }
	    
	    if($display != ''){
	        $map['display'] = $display;
	    }
	    
		$order="id desc";
		
		$tinfo = model('column')->find($tid);
		$minfo = model('models')->find($tinfo['mid']);
		$table = $minfo['name'];
		
		$col = db('column')->field('id,pid,title')->where(['mid'=>$tinfo['mid']])->order('id asc')->select();
		import('util.Tree');
		$cids = \Tree::getChildsIds($col,$tid,true);
		$tree = \Tree::getTree($col);
		
		$map['tid'] = ['in',$cids];
		
		$list = Db::name($table)->where($map)->paginate(15,false,['query'=>input('get.')]);
		$page = $list->render();
		
		$this->assign('_list', $list);
		$this->assign('_page', $page);
		$this->assign('_tinfo',$tinfo);
		$this->assign('_tree',$tree);
		
		return $this->fetch();
    }
    
    /**
    * 添加文章
    * @date: 2018年8月16日 下午1:53:59
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function add($tid=0){
        if(request()->isPost()){
            $data = input('post.');
            
            $rule = [
                'title' =>  'require',
            ];
            
            $msg = [
                'title.require' => '请填写标题',
            ];
            $validate = new Validate($rule,$msg);
            
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            
            $tinfo = model('column')->find($data['tid']);
            $minfo = model('models')->find($data['mid']);
            $table = $minfo['name'];
            $model = db($table);
            
            $data['create_time'] = NOW_TIME;
            $data['update_time'] = NOW_TIME;
            if($model->insert($data)){
                $data['aid'] = $model->getLastInsID();
                $data['mtable'] = $table;
                model('Arctiny')->arc_update($data);
                $this->success('添加成功！',url('index',['tid'=>$data['tid']]));
            }else{
                $this->error($model->getError());
            }
    
        }else{
            $tinfo = model('column')->find($tid);
            if(!empty($tinfo)){
                $minfo = model('models')->find($tinfo['mid']);
                $group = config_parse(3, $minfo['field_group']);
                $this->assign('_group',$group);
                $fields = model('fields')->where(['mid'=>$tinfo['mid'],'display'=>['in','1,2']])->order('sort asc')->select();
                $this->assign('_fields',$fields);
            }else{
                $this->assign('_group',['基础']);
                $this->assign('_fields',[]);
            }
            $this->assign('_tinfo',$tinfo);
            return $this->fetch('edit');
        }
    }
    
    /**
    * 编辑文章
    * @date: 2018年8月16日 下午5:02:42
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function edit($tid,$id){
        if(request()->isPost()){
            $data = input('post.');
        
            $rule = [
                'title' =>  'require',
            ];
        
            $msg = [
                'title.require' => '请填写标题',
            ];
            $validate = new Validate($rule,$msg);
        
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
        
            $tinfo = model('column')->find($data['tid']);
            $minfo = model('models')->find($data['mid']);
            $table = $minfo['name'];
            $model = db($table);
        
            $data['update_time'] = NOW_TIME;
            if($model->where(['tid'=>$data['tid'],'id'=>$data['id']])->update($data)){
                model('Arctiny')->arc_update($data);
                $this->success('编辑成功！',url('index',['tid'=>$data['tid']]));
            }else{
                $this->error($model->getError());
            }
        
        }else{
            $tinfo = model('column')->find($tid);
            if(!empty($tinfo)){
                $minfo = model('models')->find($tinfo['mid']);
                $group = config_parse(3, $minfo['field_group']);
                $this->assign('_group',$group);
                $fields = model('fields')->where(['mid'=>$tinfo['mid'],'display'=>['in','1,3']])->order('sort asc')->select();
                $this->assign('_fields',$fields);
                $info = db($minfo['name'])->find($id);
                $this->assign('info',$info);
            }
            $this->assign('_tinfo',$tinfo);
            return $this->fetch('edit');
        }
    }
    
    /**
     * 排序
     * @date: 2018年6月2日 上午10:48:58
     * @author: xiaocui
     * @param: variable
     * @return:
     */
    public function sort($mid=0){
        if(request()->isPost()){
            $minfo = model('models')->field('name')->find($mid);
            $table = $minfo['name'];
            $model = db($table);
            
            $post = input('post.');
            if(isset($post['ids']) && isset($post['sort'])){
                $data = [];
                foreach ($post['ids'] as $key=>$val){
                    $sort= intval($post['sort'][$key]);
                    $model->where(['id'=>$val])->update(['sort'=>$sort]);
                }
                $this->success('排序成功！');
            }
            $this->error('提交失败！');
        }
    }
    
    /**
    * 删除分组
    * @date: 2018年6月7日 上午10:50:56
    * @author: xiaocui
    * @param: $GLOBALS
    * @return:
    */
    public function del($mid,$id=0){
        if(empty($id)) $this->error('请选择要删除的记录！');
        $id = is_numeric($id)?[$id]:(array)$id;
        $minfo = model('models')->field('name')->find($mid);
        $table = $minfo['name'];
        $model = db($table);
        if($model->where(['id'=>['in',$id]])->delete()){
            db('arctiny')->where(['aid'=>['in',$id]])->delete();
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！');
        }
    }
    
    /**
     * 状态
     * @date: 2018年6月2日 上午10:48:58
     * @author: xiaocui
     * @param: variable
     * @return:
     */
    public function status($mid,$id,$display){
        $id = intval($id);
        $display = intval($display);
        $minfo = model('models')->field('name')->find($mid);
        $table = $minfo['name'];
        $model = db($table);
        if($model->where(['id'=>$id])->update(['display'=>$display])){
            $this->success('修改成功','');
        }else{
            $this->error('修改失败！');
        }
    }
    
    /**
    * 批量移动
    * @date: 2018年8月18日 下午4:33:36
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function move($mid,$tid){
        if(request()->isPost()){
            $minfo = model('models')->field('name')->find($mid);
            $table = $minfo['name'];
            
            $post = input('post.');
            if(isset($post['id']) && !empty($post['id'])){
                db($table)->where(['id'=>['in',$post['id']]])->update(['tid'=>$tid]);
                $this->success('移动成功！');
            }
            $this->error('提交失败！');
        }
    }
}
